package com.iemes.controller.client;

import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.ClientBasicFormMap;
import com.iemes.entity.ClientFormMap;
import com.iemes.mapper.client.ClientBasicMapper;
import com.iemes.mapper.client.ClientMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;
import com.iemes.util.DateUtils;

@RequestMapping("/clientelse/")
@Controller
public class ClientelseController extends BaseController {
	
	@Inject
	private ClientMapper clientMapper;
	@Inject
	private ClientBasicMapper clientBasicMapper;
	@RequestMapping("examine")
	public String getall(Model model) {
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/client/clientElse_infomation_list";
	}
	
	@RequestMapping("clientelse_findByPage")
	@ResponseBody
	public PageView clientperson_findByPage(String pageNow, String pageSize) throws Exception {
		ClientFormMap clientFormMap=getFormMap(ClientFormMap.class);
		clientFormMap=toFormMap(clientFormMap, pageNow, pageSize, clientFormMap.getStr("orderby"));
		List<ClientFormMap> clientFormMaps=clientMapper.findByPage(clientFormMap);
		pageView.setRecords(clientFormMaps);
		return pageView;
	}
	
	@RequestMapping("clientelse_edUI")
	public String clientelse_edUI(Model model){
		String id = getPara("id");
		ClientFormMap clientFormMap=clientMapper.findbyFrist("id", id, ClientFormMap.class);
		if(clientFormMap.getDate("end_compact")!=null){
			clientFormMap.put("end_compact", DateUtils.getDateTime(clientFormMap.getDate("end_compact")));
			
		}
		model.addAttribute("client", clientFormMap);
		return Common.BACKGROUND_PATH + "/client/clientElse_infomation_edit";
		
	}
	
	@RequestMapping("clientelse_editInfo")
	@ResponseBody
	public String clientelse_editInfo() {
		ClientFormMap clientFormMap=getFormMap(ClientFormMap.class);
		try {
			clientMapper.editEntity(clientFormMap);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "false";
		}
		return "success";
	}
	
	@RequestMapping("clientelse_addUI")
	public String clientelse_addUI(Model model){
		return Common.BACKGROUND_PATH + "/client/clientElse_infomation_add";
		
	}
	
	@RequestMapping("clientelse_addInfo")
	@ResponseBody
	public String clientelse_addInfo() {
		ClientFormMap clientFormMap=getFormMap(ClientFormMap.class);
		try {
			clientMapper.addEntity(clientFormMap);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "false";
		}
		return "success";
	}
	@RequestMapping("Client_IsExist")
	@ResponseBody
	public String Client_IsExist(String name){
		List<ClientFormMap> clientFormMap=clientMapper.findByAttribute("name", name, ClientFormMap.class);
		if(clientFormMap.size()>0){
			return "false";
		}
		return "true";
	}
	
	@RequestMapping("clientbasis_edUI")
	public String clientbasis_edUI(Model model){
		String id=getPara("id");
		ClientBasicFormMap clientBasisFormMap=clientBasicMapper.findbyFrist("client_id", id, ClientBasicFormMap.class);
		model.addAttribute("Clientbasic", clientBasisFormMap);
		model.addAttribute("client_id", id);
		return Common.BACKGROUND_PATH + "/client/clientbasis_infomation_add";
		
	}
	
	@RequestMapping("clientbasis_editoradd")
	@ResponseBody
	@Transactional(readOnly = false)
	public String clientbasis_editoradd() {
		ClientBasicFormMap clientBasicFormMap=getFormMap(ClientBasicFormMap.class);
		if(clientBasicFormMap.get("id")==null){
			try {
				clientBasicMapper.addEntity(clientBasicFormMap);
				ClientFormMap clientFormMap=clientMapper.findbyFrist("id", clientBasicFormMap.get("client_id").toString(), ClientFormMap.class);
				clientFormMap.put("basic_no", clientBasicFormMap.get("id"));
				clientMapper.editEntity(clientFormMap);
				return "success";
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "false";
			}
		}else {
			try {
				clientBasicMapper.editEntity(clientBasicFormMap);
				return "success";
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();return "false";
			}
		}
		
		
	}
	
	// 删除客户操作
			@ResponseBody
			@RequestMapping("clientelse_deleteEntity")
			@Transactional(readOnly = false)
			// 需要事务操作必须加入此注解
			@SystemLog(module = "客户信息管理", methods = "客户信息管理-删除信息")
			// 凡需要处理业务逻辑的.都需要记录操作日志
			public String deleteEntity() {
				String[] ids = getParaValues("ids");
				String Pid = null;
				for (String id : ids) {
					Pid = id;
					// orderMapper.deleteOrder(Integer.valueOf(id));
				}
				String[] value = Pid.split(",");
				for (String string : value) {
					try {
						clientMapper.deleteByAttribute("id", string, ClientFormMap.class);
						clientBasicMapper.deleteByAttribute("client_id", string, ClientBasicFormMap.class);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						return "false";
					}
				}
				return "success";
			}
}
